/* global toastTip:true */
import Schema from 'async-validator'
import MobileSelect from 'mobile-select'

// 表单字段
let params = {
  name: '',
  off_address_input: '',
  off_address_select: '',
  on_address_input: '',
  on_address_select: '',
  plan_start_time: '',
  phone: '',
  way: 'off', // off 接机  on 送机
  // 最终数据
  get data () {
    let data = {
      name: params.name,
      phone: params.phone,
    }

    if (params.way === 'off') {

    } else {

    }
  }
}

// 表单验证规则
let descriptor = {
  name: {
    type: 'string',
    required: true,
    message: '请输入姓名'
  },
}
// 表单验证实例
let validator = new Schema(descriptor)
// 数据代理
let dataProxy = {
  set way (value) {
    params.way = value
    // 更新出发地址,终点地址显示
    // on_address_input
    // on_address_select
    console.log(value);
    if (value === 'off') {
      // 接机
      $('[name=on_address_input]').hide()
      $('[name=on_address_select]').show()
      $('[name=off_address_input]').show()
      $('[name=off_address_select]').hide()
    } else {
      // 送机
      $('[name=on_address_input]').show()
      $('[name=on_address_select]').hide()
      $('[name=off_address_input]').hide()
      $('[name=off_address_select]').show()
    }
  },
  get way () {
    return params.way
  }
}

// 事件逻辑
var mobileSelect5 = new MobileSelect({
  trigger: '#picktime',
  title: '请选择时间',
  wheels: [
    {
      data: [
        {id: '1', value: '附近'},
        {id: '2', value: '上城区'},
        {id: '3', value: '下城区'},
        {id: '4', value: '江干区'},
        {id: '5', value: '拱墅区'},
        {id: '6', value: '西湖区'}
      ]
    },
    {
      data: [
        {id: '1', value: '1000米'},
        {id: '2', value: '2000米'},
        {id: '3', value: '3000米'},
        {id: '4', value: '5000米'},
        {id: '5', value: '10000米'}
      ]
    }
  ],
  position: [0, 0],
  transitionEnd: function (indexArr, data) {
    // console.log(indexArr);
  },
  callback: function (indexArr, data) {

  }
});

$('.way-radio .radio').on('change', function (e) {
  // 获取最新way
  dataProxy.way = $('.orderFrom')[0]['way'].value;
})

$('.submitForm').on('touchend', function (e) {
  let formJson = $('.orderFrom').serializeJSON();
  console.log(formJson)
  validator.validate(formJson, (errors, fields) => {
    if (errors) {
      // validation failed, errors is an array of all errors
      // fields is an object keyed by field name with an array of
      // errors per field
      // return handleErrors(errors, fields);
      // 验证失败
      console.log(errors, fields)
      toastTip.show(errors[0].message)
      return
    }
    // 验证通过
    console.log(errors, fields)
  })
})
